<?php
// Converts JSON plans of study to XML.

exit();

session_start();
if ( ! isset( $_SESSION['adminLoggedIn'] ) ) {
   exit;
}

require_once("db.php");

$idList = my_mysql_query("select id, json as plan FROM gs_plans");
while ( $row = mysql_fetch_object($idList) ) {
   
   // Convert the JSON plan of study to XML.  Note that this builds
   // a "flat" XML forest of <semester> and <course> elements.  That means
   // the course elements are siblings of semester, not children.
   // Sample JSON:
   //    [ {"id": "2008-1", "type": "semester", "name": "Winter 2008"}, 
   //      {"id": "cs500", "type": "course", "name": "CS 500"} ]  
   // There are anomalies in the JSON encoding of some plans of study.  This was
   // caused by IE :-(  In some cases, the "type" field is null.  However, the
   // name and id elements are still intact.
   // Fubar'd JSON:
   //
   //    [ {"id": "2009-1", "type": "null", "name": "Winter 2009 "},
   //      {"id": "cs613", "type": "null", "name": "CS 613 "},
   //      {"id": "2009-2", "type": "null", "name": "Summer 2009 "},
   //      {"id": "cs612", "type": "null", "name": "CS 612 "} ]
   
   $json = json_decode( $row->plan );
   
   // Make sure there is a plan to work with.
   if ( count( $json ) > 0 ) {
      $xmlPlan = "";
      foreach( $json as $item ) {
         if ( substr( $item->id, 0, 2 ) != "cs" ) {  // found a semester
            $xmlPlan .= '<semester label="' . trim($item->name) . '" />';
         }
         else {
            $xmlPlan .= '<course label="' . trim($item->name) . '" />';
         }
      }
      
      // Now store the XML plan of study.
      my_mysql_query("update gs_plans set xml='$xmlPlan' where id=$row->id");

echo $xmlPlan . "\n";
   }
}

?>